home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Multiplatform Code⁄Data Sharing / HelloBothWorlds / Libraries / UnivPictLoader.cpp < prev    next >
Encoding:
Text File  |  1997-06-26  |  2.0 KB  |  82 lines  |  [TEXT/CWIE]

  1.  
  2. // mail <chelly@eden.com> or surf http://www.eden.com/~chelly for feedback
  3. // free source code - do whatever you like with it
  4.  
  5. // loader for graphic elements that uses either mac or universal pictures
  6.  
  7. #include "UnivPictLoader.h"
  8.  
  9. #include "common.h"
  10. #include "gepicture.h"
  11.  
  12. // Graphic Elements
  13. #include "gelemnts.h"
  14. #include "Rects.h"
  15.  
  16. GE_CALLBACK( Boolean, LoadUnivPictElement)( GEWorldPtr world, GrafElPtr element,
  17.         short startResNum, short nResources )
  18. {
  19.     Boolean ok = true;
  20.     GWorldPtr gWorld = nil;
  21.     PixMapHandle pixH = nil;
  22.     short err = 0;
  23.     
  24.     {
  25.         element->resID = startResNum;
  26.         
  27.         // if element is already loaded, use its offscreen
  28.         for ( GrafElPtr testElem = world->idList; testElem; testElem = testElem->nextByID )
  29.             if ( testElem->resID == startResNum )
  30.             {
  31.                 element->graphWorld = testElem->graphWorld;
  32.                 element->graphRect = testElem->graphRect;
  33.                 return true;
  34.             }
  35.         
  36.         // get image size
  37.         GEPicture* const pic = GetGEPicture( startResNum );
  38.         if ( !pic ) goto abort;
  39.         Rect box = GetGEPictureFrame( pic );
  40.         DisposeGEPicture( pic );
  41.         
  42.         // set up element
  43.         element->graphRect = box;
  44.         Rect totalBox = box;
  45.         totalBox.bottom *= nResources;    // multi-frame element support
  46.         
  47.         // draw image(s) to offscreen
  48.         err = NewGWorld( &gWorld, offscrnDepth, &totalBox, world->worldCTable, 0, 0 );
  49.         if ( err ) goto abort;
  50.         pixH = GetGWorldPixMap( gWorld );
  51.         if ( !pixH ) goto abort;
  52.         if ( !LockPixels( pixH ) ) goto abort;
  53.         NoPurgePixels( pixH );
  54.         CGrafPtr save_port;
  55.         GDHandle save_dev;
  56.         GetGWorld( &save_port, &save_dev );
  57.         SetGWorld( gWorld, nil );
  58.         
  59.         for ( int counter = 0; counter < nResources; ++counter )
  60.         {
  61.             GEPicture* const pic = GetGEPicture( startResNum + counter );
  62.             if ( pic )
  63.             {
  64.                 DrawGEPicture( pic, &box );
  65.                 DisposeGEPicture( pic );
  66.                 RectOffset( &box, 0, RectHeight(&box) );
  67.             }
  68.         }
  69.         SetGWorld( save_port, save_dev );
  70.  
  71.         element->graphWorld = gWorld;
  72.         goto done;
  73.     }
  74.  
  75. abort:
  76.     if ( gWorld ) DisposeGWorld( gWorld );
  77.     ok = false;
  78. done:
  79.     return ok;
  80. }
  81.  
  82.